package org.example.redpacket.controller;

import org.example.redpacket.handler.R;
import org.example.redpacket.model.dto.UserRedPacketDetailDTO;
import org.example.redpacket.model.vo.UseRedPacketVO;
import org.example.redpacket.service.UserRedPacketDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 用户和红包 相关的 Controller
 *
 * @author: hero生仔
 */
@RestController
@RequestMapping("/red-packet")
public class RedPacketController {

    @Autowired
    private UserRedPacketDetailService userRedPacketDetailService;

    @PostMapping("/useRedPacket")
    public R useRedPacket(@Valid @RequestBody UserRedPacketDetailDTO userRedPacketDetailDTO) {
        if (userRedPacketDetailDTO.getRedPacketList().isEmpty()) {
            return R.fail("用户" + userRedPacketDetailDTO.getUserId() + " 使用红包失败");
        }
        String flag = userRedPacketDetailService.batchUpdateRedPacketDetail(userRedPacketDetailDTO);
        return R.ok().setData(UseRedPacketVO.builder().useFlag(flag).build());
    }
}
